package controller;

import implementor.CategoryImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.JOptionPane;

import daoInterface.CategoryDao;
import dto.Category;


import utilities.Constants;
import utilities.ViewUtilites;
import views.inventory.AddNewCategory;

public class AddNewCategoryController implements ActionListener{
	private AddNewCategory view;
	private Category model;
	
	public AddNewCategoryController() {
		
	}

	public AddNewCategoryController(AddNewCategory view, Category model) {
		super();
		this.view = view;
		this.model = model;
		view.actionlisteners(this);

	}

	public AddNewCategory getView() {
		return view;
	}

	public void setView(AddNewCategory view) {
		this.view = view;
	}

	public Category getModel() {
		return model;
	}

	public void setModel(Category model) {
		this.model = model;
	}
	
	public void getParentId() throws Exception
	{
		CategoryDao cd=new CategoryImp();
		Category[] c=cd.retrieveAll();
		String[] items=new String[c.length+1];
		items[0]="--Select Parent Id--";
		for(int i=1;i<c.length;i++)
		{
			items[i]=c[i-1].getParentLeafId();
		}
		ViewUtilites.set_comboBox(view.getCbId(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getCbId());		
		
	}

	public void reactOnSave()
	{
		String catname=view.gettAddCatName().getText();
		String parentId=view.getTparentLevel().getSelectedItem().toString();
		String catcode=null;
		int leaflevel=0;
		String leafid=null;
		model=new Category(catcode, leaflevel, leafid, catname, parentId, Constants.ACTIVE);
		CategoryDao c=new CategoryImp();
		try
		{
			c.add(model);
			JOptionPane.showMessageDialog(null, "Data successfully saved!");
		}
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Catagory Name already exist, please use another one");
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully Added,Please try again !");
		}
	
	}
	
	public void reactOnUpdate() throws Exception
	{
		String leafname=view.gettEditCatName().getText();
		CategoryDao cd=new CategoryImp();
		Category c=cd.retrieve("leafName", leafname);
		if(view.getChckbxMarkInactive().isSelected())
			c.setStatus(Constants.INACTIVE);
		try
		{
			cd.update(c);
			JOptionPane.showMessageDialog(null, "Data successfully Updated.");
			
		}
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Catagory Name already exist, please use another one");
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully Added,Please try again !");
		}
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String k=e.getActionCommand();
		
		if(k.contains("SAVE"))
		{
			this.reactOnSave();
		}
		
		if(k.contains("UPDATE"))
		{
			try {
				reactOnUpdate();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		
	}

	
	
	
	
	

}
